package com.xr.org.emp;

import java.util.List;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.utils.*;
import com.bstek.dorado.utils.variant.VariantSet;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;
import com.xr.common.CommUtil;
import com.xr.common.Tools;
import com.xr.hr.personnel.publicClass;

/**
 * newEmpFormViewModel
 */
public class newEmpFormViewModel extends DefaultViewModel {
	
	protected void initDataset(ViewDataset dataset) throws Exception {
		super.initDataset(dataset);
		if(dataset.getId().equals("datasetEmp")){
		    CommUtil.formFieldShow(dataset,"org_employee");
		}
	}
 
 
protected void initDatasets()throws Exception {
    super.initDatasets();

  }

  public void empSave(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {
	  DBStatement dbs = new DBStatement();
		Dataset dsEmp = getDataset("datasetEmp"); 
		Dataset dsWorkHis=getDataset("datasetWorkHis");
		Dataset dsEducate=getDataset("datasetEducate");
		Dataset dsFamily=getDataset("datasetFamily");
		Dataset dsTrain=getDataset("datasetTrain");
		Dataset dsBank=getDataset("datasetBank");
		Dataset dsCertificate=getDataset("datasetCertificate");
		String pcStr="";
		publicClass pc=new publicClass();
		pc.setEmployeeId((dsEmp.getString("EMPLOYEE_ID")==null||dsEmp.getString("EMPLOYEE_ID").equals(""))?"0":dsEmp.getString("EMPLOYEE_ID"));
		pc.setIdCardType(dsEmp.getInt("ID_CARD_TYPE"));
		pc.setIdCardNum(dsEmp.getString("ID_CARD_NUM"));
		pcStr=pc.checkIdCardNum(); 
		if(!pcStr.equals("")){
			throw new Exception(pcStr);
		}
		if(StringHelper.toValidString( dsEmp.getString("BIRTHDAY"))==""&&pc.getBirthday()!=""){
			 dsEmp.setDate("BIRTHDAY", Tools.formatDate(pc.getBirthday(),"yyyy-MM-dd"));
			 
		}
			
		dsEmp.setValue("EMP_SELF_IU", 1);
	CommUtil.dbsUpdateData(dbs, dsEmp, "ORG_EMPLOYEE", "EMPLOYEE_ID","seq_org");
	dsEmp.parameters().setValue("EMPLOYEE_ID", dsEmp.getString("EMPLOYEE_ID"));
	
		CommUtil.executeUpdate(dbs, "update org_employee set BIRTH_YEAR=f_getlunar_char(BIRTHDAY) where BIRTHDAY is not null and BIRTH_YEAR is null and EMPLOYEE_ID="+dsEmp.getString("EMPLOYEE_ID"));
		CommUtil.executeUpdate(dbs, "update org_employee set BIRTHDAY_LUNAR=f_getlunar_date(BIRTHDAY) where BIRTHDAY is not null and BIRTHDAY_LUNAR is null and EMPLOYEE_ID="+dsEmp.getString("EMPLOYEE_ID"));
		//CommUtil.executeUpdate(dbs, "update org_employee set TEMP_PWD=fn_md5(TEMP_PWD) where EMPLOYEE_ID="+dsEmp.getString("EMPLOYEE_ID"));
		setId(dsWorkHis,dsEmp.getString("EMPLOYEE_ID"));
		CommUtil.dbsUpdateData(dbs, dsWorkHis, "EMP_WORKHIS", "EMP_WORKHIS_ID","seq_org");
		
		RecordIterator ri = dsWorkHis.recordIterator();
		ri.setVisibility(Dataset.FILTER_DELETED);
		while (ri.hasNext()) {
		  Record record = ri.nextRecord();
		  dbs.setSql("delete from EMP_WORKHIS where EMP_WORKHIS_ID="+record.getString("EMP_WORKHIS_ID"));
		  dbs.execute();
		}
		
		setId(dsEducate,dsEmp.getString("EMPLOYEE_ID"));
		CommUtil.dbsUpdateData(dbs, dsEducate, "EMP_EDUCATE", "EMP_EDUCATE_ID","seq_org");
		ri = dsEducate.recordIterator();
		ri.setVisibility(Dataset.FILTER_DELETED);
		while (ri.hasNext()) {
		  Record record = ri.nextRecord();
		  dbs.setSql("delete from EMP_WORKHIS where EMP_WORKHIS_ID="+record.getString("EMP_WORKHIS_ID"));
		  dbs.execute();
		}
		setId(dsFamily,dsEmp.getString("EMPLOYEE_ID"));
		CommUtil.dbsUpdateData(dbs, dsFamily, "EMP_FAMILY", "EMP_FAMILY_ID","seq_org");
		ri = dsFamily.recordIterator();
		ri.setVisibility(Dataset.FILTER_DELETED);
		while (ri.hasNext()) {
		  Record record = ri.nextRecord();
		  dbs.setSql("delete from EMP_FAMILY where EMP_FAMILY_ID="+record.getString("EMP_FAMILY_ID"));
		  dbs.execute();
		}
		setId(dsTrain,dsEmp.getString("EMPLOYEE_ID"));
		CommUtil.dbsUpdateData(dbs, dsTrain, "EMP_TRAIN", "EMP_TRAIN_ID","seq_org");
		ri = dsTrain.recordIterator();
		ri.setVisibility(Dataset.FILTER_DELETED);
		while (ri.hasNext()) {
		  Record record = ri.nextRecord();
		  dbs.setSql("delete from EMP_TRAIN where EMP_TRAIN_ID="+record.getString("EMP_TRAIN_ID"));
		  dbs.execute();
		}
		setId(dsBank,dsEmp.getString("EMPLOYEE_ID"));
		CommUtil.dbsUpdateData(dbs, dsBank, "EMP_BANK", "BANK_ID","seq_org");
		ri = dsBank.recordIterator();
		ri.setVisibility(Dataset.FILTER_DELETED);
		while (ri.hasNext()) {
		  Record record = ri.nextRecord();
		  dbs.setSql("delete from EMP_BANK where BANK_ID="+record.getString("BANK_ID"));
		  dbs.execute();
		}
		setId(dsCertificate,dsEmp.getString("EMPLOYEE_ID"));
		CommUtil.dbsUpdateData(dbs, dsCertificate, "emp_certificate", "CERTIFICATE_ID","seq_org");
		ri = dsCertificate.recordIterator();
		ri.setVisibility(Dataset.FILTER_DELETED);
		while (ri.hasNext()) {
		  Record record = ri.nextRecord();
		  dbs.setSql("delete from emp_certificate where CERTIFICATE_ID="+record.getString("CERTIFICATE_ID"));
		  dbs.execute();
		}
		outParameters.setValue("employeeId", dsEmp.getString("EMPLOYEE_ID"));
   // super.doUpdateData(parameters, outParameters);
  }

  private void setId(Dataset ds,String id){
	  ds.moveFirst();
		while (!ds.isLast()) {
			ds.setRecordState(Record.STATE_NEW);
			ds.setString("EMPLOYEE_ID",  id);
			ds.moveNext();
		}
  }
  
}
